package com.hengpeng.itfints.webcache;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.constructs.blocking.BlockingCache;

/**
 * <pre>
 * 页面缓存工具类
 * </pre>
 * @author chenjian
 * @version 1.0, 2012-5-29
 */
public class PageCacheUtils
{
	/**
	 * 所有页面缓存Map,key:过滤器名称,value:缓存对象
	 */
	private static final Map<String, BlockingCache> cacheMap = new HashMap<String, BlockingCache>();

	private static final PageCacheUtils pageCacheUtils = new PageCacheUtils();

	private PageCacheUtils()
	{

	}

	public static PageCacheUtils getInstance()
	{
		return pageCacheUtils;
	}

	protected void addPageCache(String filterName, BlockingCache chache)
	{
		cacheMap.put(filterName, chache);
	}

	/**
	 * <pre>
	 * 清空页面缓存
	 * </pre>
	 * @param filterName 页面缓存过滤器名称
	 * @param requestURI 页面路径,注意加上根路径,比如/itfints/index.htm
	 */
	@SuppressWarnings("unchecked")
	public void remove(String filterName, String requestURI)
	{
		BlockingCache cache = cacheMap.get(filterName);
		if (cache != null)
		{
			List<String> list = cache.getKeys();
			for (String key : list)
			{
				if (key.startsWith(requestURI))
				{
					cache.remove(key);
				}
			}
		}
	}
}
